 function [xcell,xm]=out_vdec(xmat,rownames,colnames,titulo,pvec)
% =========================================================================
% OUT_VDEC   
% xmat [nr nc nd] 
% rownames, [nr 1] cell with names of rows 
% colnames, [nc 1] cell with names of columns 
% titulo, cell with title 
% pvec [2 1] percentiles to report, default is [0.05 0.95] 
% =========================================================================
% Alejandro Justiniano  10/15/07
% =============================================================
sizx=size(xmat); 
if length(sizx)~=3;error('OUT_VDEC is for 3 dimensional matrices only');end 
nr=sizx(1);nc=sizx(2);nd=sizx(3);clear sizx; 
if length(rownames)~=nr;error('Mismatch ROWNAMES and num rows');end 
if length(colnames)~=nc;error('Mismatch COLNAMES and num columns');end 
% =======================
% Compute Matrix Moments 
% =======================
if nargin < 5 || isempty(pvec); 
        pvec=[0.05;0.95];
end 
pind=round(pvec*nd); 
% Obtain mean, median and PVEC percentiles 
xmed =squeeze(median(xmat,3));
xmean=squeeze(mean(xmat,3));
xmat=sort(xmat,3);
x1=squeeze(xmat(:,:,pind(1) ) );
x2=squeeze(xmat(:,:,pind(2) ) );
clear xmat pind;
% Create cell
xcell = emptycell(2*nr+2,nc+1 ); 
desc=[num2str(100*pvec(1)),'%,',num2str(100*pvec(2)),'%'];
xcell(1,1)=cellstr(titulo);xcell(1,2:3)={'Median',desc}; 
xcell(2,1)={'Series'}; 
xcell(2,2:end)=(colnames(:))'; 
clear rind; 
rind=3:2:(2*nr+1); 
for ii=1:nr; 
    xcell(rind(ii),1)=rownames(ii); 
    for jj=1:nc; 
        xcell(rind(ii),jj+1)={sprintf('    %5.2f',xmed(ii,jj))}; 
        xcell(rind(ii)+1,jj+1) = {['[',sprintf('%5.2f',x1(ii,jj)),',',sprintf('%5.2f', x2(ii,jj)),']'] }; 
    end 
end 
if nargout==2
    xm.med=xmed; 
    xm.mean=xmean; 
    xm.p1=x1; 
    xm.p2=x2 ; 
end 